package org.example.musicserver.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.musicserver.common.Result;
import org.example.musicserver.entity.Information;
import org.example.musicserver.mapper.InformationMapper;
import org.example.musicserver.service.InformationService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InformationServiceImpl extends ServiceImpl<InformationMapper, Information> implements InformationService {
    @Resource
    private InformationMapper informationMapper;

    @Override
    public Result addInformation(Information Information) {
        if (informationMapper.insert(Information) > 0) {
            return Result.success("添加成功", true);
        } else {
            return Result.error("添加失败");
        }
    }

    @Override
    public Result deleteInformation(Integer InformationId) {
        if (informationMapper.deleteById(InformationId) > 0) {
            return Result.success("添加成功", true);
        } else {
            return Result.error("添加失败");
        }
    }

    @Override
    public Result updateInformation(Information Information) {
        if (informationMapper.updateById(Information) > 0) {
            return Result.success("添加成功", true);
        } else {
            return Result.error("添加失败");
        }
    }

    @Override
    public Result selectInformation(String query) {
        QueryWrapper<Information> queryWrapper = new QueryWrapper<>();
        if(query != null){
            queryWrapper.like("title",query);
        }
        queryWrapper.orderByDesc("create_time");
        List<Information> Informations = informationMapper.selectList(queryWrapper);
        return Result.success("返回结果成功",Informations);
    }

    @Override
    public Result detail(Integer id) {
        return Result.success("返回结果成功",informationMapper.selectById(id));
    }
}
